#include "pam_pg_su.h"
#include "pgconn.h"

/* ****************************************
 * pam_sm_authenticate
 * Funcao que faz autenticacao para o PAM / Modulo para verificar grupo possivel
 * ****************************************/
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
                                    int argc, const char **argv) {

   const char *user=NULL;
   CONNECTION_DB conn;
   modopt_t modopt;
   int retval,uid=0,verifywheel=0;

   modopt = mod_options(argc, argv);

   retval = pam_get_item(pamh, PAM_RUSER, (const void**) user );
   if(retval != PAM_SUCCESS)
      return PAM_USER_UNKNOWN;

   conn = init_db(modopt);
   uid = get_user_id(conn, user);
   verifywheel = verify_wheel(conn, user);
   free_db(conn);

   if(uid==0 || verifywheel==1)
      return PAM_SUCCESS;

   return PAM_AUTH_ERR;

}

PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags,
                               int argc, const char **argv) {

   return (PAM_SUCCESS);

}
